package ckw.database.model;

import java.io.Serializable;
import java.util.ArrayList;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Type;

import core.communication.Direction;
import core.communication.message.ConfigurationNode;
import core.database.EntityAction;
import core.database.Node;
import core.util.Converter;

@Entity
@Table(name = "nodesChanges")
public class NodesChange implements Serializable {
	private static final long serialVersionUID = -4301768319427466634L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = null;
	@Column(name = "uri")
	private String uri;
	
	@Transient
	private ArrayList<ConfigurationNode> nodeActions = new ArrayList<ConfigurationNode>();
	@Column(name="nodeActionsXML")
	@Type(type="text")
	private String nodeActionsXML;
	
	
//	@Column(name="locatorUrisChangesXML")
//	@Type(type = "text")
//	private String locatorUrisChangesXML;
//	@Transient
//	private List<LocatorUriChanges> locatorUrisChanges = new ArrayList<LocatorUriChanges>();
//	@Column(name="deletedNodesXML")
//	@Type(type = "text")
//	private String deletedNodesXML;
//	@Transient
//	private List<String> deletedNodes = new ArrayList<String>();
//	@Column(name="addedNodesXML")
//	@Type(type = "text")
//	private String addedNodesXML;
//	@Transient
//	private List<String> addedNodes = new ArrayList<String>();
	
	public NodesChange() {}
	
	public NodesChange(String locatorUri) {
		this.uri = locatorUri;
	}

	public Long getId() {
		return id;
	}

	@SuppressWarnings("unused")
	private void setId(Long id) {
		this.id = id;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public ArrayList<ConfigurationNode> getNodeActions() {
		return nodeActions;
	}

	public void setNodeActions(ArrayList<ConfigurationNode> nodeActions) {
		this.nodeActions = nodeActions;
	}

	public String getNodeActionsXML() {
		return nodeActionsXML;
	}

	public void setNodeActionsXML(String nodeActionsXML) {
		this.nodeActionsXML = nodeActionsXML;
	}
	
	public void convertActionsToXML() {
		nodeActionsXML = Converter.objectToXML(nodeActions);
	}
	
	@SuppressWarnings("unchecked")
	public void convertActionsToObject() {
		nodeActions = (ArrayList<ConfigurationNode>)Converter.XMLToObject(nodeActionsXML);
	}
	
	// Delete
	// AddParent
	public void addAction(EntityAction action, String targetNode) {
		nodeActions.add(new ConfigurationNode(action, targetNode));
	}
	
	// Add
	public void addAction(EntityAction action, Node node, Direction direction) {
		nodeActions.add(new ConfigurationNode(action, convertToCoreNode(node), direction));
	}
	
	// Edit
	public void addAction(EntityAction action, String targetNode, Node node) {
		nodeActions.add(new ConfigurationNode(action, targetNode, convertToCoreNode(node)));
	}
	
	private Node convertToCoreNode(Node node) {
		Node coreNode = new Node();
		coreNode.setAddress(node.getAddress());
		coreNode.setIp(node.getIp());
		coreNode.setName(node.getName());
		coreNode.setNodeType(node.getNodeType());
		coreNode.setPort(node.getPort());
		return coreNode;
	}

//	public String getLocatorUrisChangesXML() {
//		return locatorUrisChangesXML;
//	}
//
//	public void setLocatorUrisChangesXML(String locatorUrisChangesXML) {
//		this.locatorUrisChangesXML = locatorUrisChangesXML;
//	}
//
//	public String getDeletedNodesXML() {
//		return deletedNodesXML;
//	}
//
//	public void setDeletedNodesXML(String deletedNodesXML) {
//		this.deletedNodesXML = deletedNodesXML;
//	}
//
//	public String getAddedNodesXML() {
//		return addedNodesXML;
//	}
//
//	public void setAddedNodesXML(String addedNodesXML) {
//		this.addedNodesXML = addedNodesXML;
//	}
//
//	public List<LocatorUriChanges> getLocatorUrisChanges() {
//		return locatorUrisChanges;
//	}
//
//	public void setLocatorUrisChanges(List<LocatorUriChanges> locatorUrisChanges) {
//		this.locatorUrisChanges = locatorUrisChanges;
//	}
//
//	public List<String> getDeletedNodes() {
//		return deletedNodes;
//	}
//
//	public void setDeletedNodes(List<String> deletedNodes) {
//		this.deletedNodes = deletedNodes;
//	}
//
//	public List<String> getAddedNodes() {
//		return addedNodes;
//	}
//
//	public void setAddedNodes(List<String> addedNodes) {
//		this.addedNodes = addedNodes;
//	}
//	
//	@PrePersist
//	@PreUpdate
//	private void convertListsToXml() {
//		locatorUrisChangesXML = Converter.objectToXML(locatorUrisChanges);
//		deletedNodesXML = Converter.objectToXML(deletedNodes);
//		addedNodesXML = Converter.objectToXML(addedNodes);
//	}
//	
//	@PostConstruct
//	@PostLoad
//	private void converList() {
//		locatorUrisChanges = (List<LocatorUriChanges>)Converter.XMLToObject(locatorUrisChangesXML);
//		deletedNodes = (List<String>)Converter.XMLToObject(deletedNodesXML);
//		addedNodes = (List<String>)Converter.XMLToObject(addedNodesXML);
//	}
}
